import { Model, DataTypes, Optional } from 'sequelize';
import sequelize from '@/config/database';

// 定义任务日志属性接口
interface JobLogAttributes {
  jobLogId: number;
  jobName: string;
  jobGroup: string;
  invokeTarget: string;
  jobMessage?: string;
  status?: string;
  exceptionInfo?: string;
  createTime?: Date;
}

// 创建时可选的字段
interface JobLogCreationAttributes extends Optional<JobLogAttributes, 'jobLogId'> {}

// 任务日志模型类
class JobLog extends Model<JobLogAttributes, JobLogCreationAttributes> implements JobLogAttributes {
  public jobLogId!: number;
  public jobName!: string;
  public jobGroup!: string;
  public invokeTarget!: string;
  public jobMessage?: string;
  public status?: string;
  public exceptionInfo?: string;
  public createTime?: Date;

  // 获取任务执行时长（已移除）
}

JobLog.init(
  {
    jobLogId: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
      field: 'job_log_id'
    },
    jobName: {
      type: DataTypes.STRING(64),
      allowNull: false,
      field: 'job_name'
    },
    jobGroup: {
      type: DataTypes.STRING(64),
      allowNull: false,
      field: 'job_group'
    },
    invokeTarget: {
      type: DataTypes.STRING(500),
      allowNull: false,
      field: 'invoke_target'
    },
    jobMessage: {
      type: DataTypes.STRING(500),
      field: 'job_message'
    },
    status: {
      type: DataTypes.STRING(1),
      defaultValue: '0',  // 0=正常,1=失败
      field: 'status'
    },
    exceptionInfo: {
      type: DataTypes.STRING(2000),
      field: 'exception_info'
    },
    createTime: {
      type: DataTypes.DATE,
      field: 'create_time'
    }
  },
  {
    sequelize,
    tableName: 'sys_job_log',
    timestamps: false
  }
);

export default JobLog; 